/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import who.tmap.toolkit.BaseMapStrokeWidth;

/**
 * @author stl
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class JDialogBaseMapLineWidth extends JDialog {

    private boolean okClicked =false;
    private boolean dataHasBeenUpdated =false;
    private double lineWidth;  //  @jve:decl-index=0:
    
	private javax.swing.JPanel jContentPane = null;
	
	private JPanel jPanelFields = null;
	private JButton jButtonOK = null;
	private JLabel jLabel = null;
	private JPanel jPanelButtons = null;
	private JButton jButtonCancel = null;
	private JComboBox jComboBoxBackgroundColor = null;

	
	/**
	 * This is the default constructor
	 */
	public JDialogBaseMapLineWidth( Frame parent ) {
		super( parent );
		initialize();
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setTitle("Base Map Line Width");
		this.setSize(340, 154);
		this.setContentPane(getJContentPane());
		this.setLocationRelativeTo( null );
		
		getRootPane().setDefaultButton( getJButtonOK() );
	}
	
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getJContentPane() {
		if(jContentPane == null) {

			GridLayout gridLayout1 = new GridLayout();
			jContentPane = new javax.swing.JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.setBorder(javax.swing.BorderFactory.createEmptyBorder(5,5,5,5));		
			jContentPane.add(getJPanelFields(), BorderLayout.NORTH);
			jContentPane.add(getJPanelButtons(), BorderLayout.SOUTH);
			
		}
		return jContentPane;
	}
	
	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */    
	private JPanel getJPanelFields() {
		if (jPanelFields == null) {
			jPanelFields = new JPanel();
			GridLayout gridLayout3 = new GridLayout(2,1);
			
			jPanelFields.setLayout(gridLayout3);		    
		    
			jLabel = new JLabel();
			jLabel.setText("Please select the base map line width");
			
			jPanelFields.add(jLabel);
			jPanelFields.add(getJComboBoxBackgroundColor());

			jPanelFields.setBorder(javax.swing.BorderFactory.createEmptyBorder(0,0,5,0));

		}
		return jPanelFields;
	}
	
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getJButtonOK() {
		if (jButtonOK == null) {
			jButtonOK = new JButton();
			jButtonOK.setText("OK");
			
			jButtonOK.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {    
				    jButtonOK_actionPerformed();
				}
			});
		}

		return jButtonOK;
	}
	
	private void jButtonOK_actionPerformed() {
	    okClicked =true;
	    if ( dataHasBeenUpdated ) {
	    	lineWidth =BaseMapStrokeWidth.values[ (getJComboBoxBackgroundColor()).getSelectedIndex() ];
	    }
	    setVisible( false );
	}
	
	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */    
	private JPanel getJPanelButtons() {
		if (jPanelButtons == null) {
			jPanelButtons = new JPanel();
			jPanelButtons.add(getJButtonOK(), null);
			jPanelButtons.add(getJButtonCancel(), null);
		}
		return jPanelButtons;
	}
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getJButtonCancel() {
		if (jButtonCancel == null) {
			jButtonCancel = new JButton();
			jButtonCancel.setText("Cancel");
			jButtonCancel.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {    
				    setVisible( false );
				}
			});
		}
		return jButtonCancel;
	}
	
	public boolean okHasBeenClicked() {
	    return okClicked; 
	}
	
	public boolean dataHasChanged() {
	    return dataHasBeenUpdated;
	}

	/**
	 * This method initializes jComboBoxBackgroundColor	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBoxBackgroundColor() {
		if (jComboBoxBackgroundColor == null) {
			jComboBoxBackgroundColor = new JComboBox( BaseMapStrokeWidth.names );
			jComboBoxBackgroundColor.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					dataHasBeenUpdated =true;
				}
			});
		}
		return jComboBoxBackgroundColor;
	}

	public double getLineWidth() {
		return lineWidth;
	}

	public void setLineWidth(double lineWidth) {
		this.lineWidth = lineWidth;
		
		for( int ct =0; ct < BaseMapStrokeWidth.values.length; ct++ ) {
			if ( lineWidth == BaseMapStrokeWidth.values[ ct ] ) {
				getJComboBoxBackgroundColor().setSelectedIndex( ct );
				return;
			}
		}		
		
		getJComboBoxBackgroundColor().setSelectedItem( BaseMapStrokeWidth.names[ BaseMapStrokeWidth.DEFAULT_IDX] );
	}	

  }  //  @jve:decl-index=0:visual-constraint="10,10"
